package com.ruoyi.common.utils;

import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XlslUtil {
    //获取工作铺
    public static Workbook getWorkBook(MultipartFile file) {
        // 获得文件名
        String fileName = file.getOriginalFilename();
        // 创建Workbook工作薄对象，表示整个excel
        Workbook workbook = null;
        try {
            // 获取excel文件的io流
            InputStream is = file.getInputStream();
            // 根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
            if (fileName.endsWith("xls")) {
                // 2003
                return new HSSFWorkbook(is);
            } else if (fileName.endsWith("xlsx")) {
                // 2007
                return new XSSFWorkbook(is);
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        return workbook;
    }

    /**
     * 获取 Excel 表格中的图片（兼容 .xls 和 .xlsx 格式）
     *
     * @param sheet Excel 工作表
     * @return 图片数据 Map，键为单元格引用（例如 "0:0"），值为图片数据
     */
    public static Map<String,  List<PictureData>> getSheetPictures(Sheet sheet) {
        Map<String, List<PictureData>> sheetPictures = new HashMap<>();

        // 检查是否有绘图对象
        if (sheet instanceof XSSFSheet) {
            // XSSFWorkbook (.xlsx)
            XSSFSheet xssfSheet = (XSSFSheet) sheet;
            XSSFDrawing drawingPatriarch = xssfSheet.getDrawingPatriarch();
            if (drawingPatriarch != null) {
                for (org.apache.poi.xssf.usermodel.XSSFShape shape : drawingPatriarch.getShapes()) {
                    if (shape instanceof XSSFPicture) {
                        XSSFPicture picture = (XSSFPicture) shape;
                        ClientAnchor anchor = picture.getClientAnchor();

                        // 计算单元格引用
                        int row1 = anchor.getRow1();
                        int col1 = anchor.getCol1();
                        String cellReference = row1 + ":" + col1; // key
                        if (sheetPictures.containsKey(cellReference)){
                            sheetPictures.get(cellReference).add(picture.getPictureData());
                        }else {
                            ArrayList<PictureData> pictureData = new ArrayList<>();
                            pictureData.add(picture.getPictureData());
                            sheetPictures.put(cellReference,pictureData);
                        }
                    }
                }
            }
        } else if (sheet instanceof HSSFSheet) {
            // HSSFWorkbook (.xls)
            HSSFSheet hssfSheet = (HSSFSheet) sheet;
            List<HSSFShape> shapes = hssfSheet.getDrawingPatriarch().getChildren();
            if (shapes != null) {
                for (HSSFShape shape : shapes) {
                    if (shape instanceof org.apache.poi.hssf.usermodel.HSSFPicture) {
                        org.apache.poi.hssf.usermodel.HSSFPicture picture = (org.apache.poi.hssf.usermodel.HSSFPicture) shape;
                        ClientAnchor anchor = picture.getClientAnchor();
                        // 计算单元格引用
                        int row1 = anchor.getRow1();
                        int col1 = anchor.getCol1();
                        String cellReference = row1 + ":" + col1; // 假设每个图片只覆盖一个单元格

                        if (sheetPictures.containsKey(cellReference)){
                            sheetPictures.get(cellReference).add(picture.getPictureData());
                        }else {
                            ArrayList<PictureData> pictureData = new ArrayList<>();
                            pictureData.add(picture.getPictureData());
                            sheetPictures.put(cellReference,pictureData);
                        }
                    }
                }
            }
        }
        return sheetPictures;
    }


}
